package com.swiftride.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swiftride.pojo.domain.DriverContract;
import com.swiftride.pojo.dto.DriverContractDto;
import com.swiftride.pojo.query.PageQueryWrapper;
import com.swiftride.result.PageResult;
import com.swiftride.result.R;
import com.swiftride.service.IDriverContractService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "司机合同表", description = "司机合同表")
@RestController
@RequestMapping("/manager/driverContract")
public class DriverContractController {

    @Autowired
    public IDriverContractService driverContractService;

    @Operation(summary = "上传合同", description = "上传合同接口")
    @Parameter(name = "driverContractDto", description = "合同信息", required = true)
    @Parameter(name = "file", description = "合同文件", required = true)
    @PostMapping("/upload")
    public R<Void> upload(@RequestPart("file") MultipartFile file
            , @RequestPart("data") DriverContractDto driverContractDto) {
        driverContractService.upload(driverContractDto, file);
        return R.success();
    }

    @Operation(summary = "作废合同", description = "作废合同接口")
    @Parameter(name = "contractId", description = "合同id", required = true)
    @GetMapping("/cancel/{contractId}")
    public R<Void> cancel(@PathVariable Long contractId) {
        driverContractService.cancel(contractId);
        return R.success();
    }

    /**
     * --------------------------------------基础方法--------------------------------------
     **/
    @Operation(summary = "保存DriverContract", description = "基础对象保存接口")
    @Parameter(name = "driverContract", description = "保存的对象", required = true)
    @PostMapping
    public R<Void> save(@RequestBody @Valid DriverContract driverContract) {
        return R.success(driverContractService.save(driverContract));
    }

    @Operation(summary = "修改DriverContract", description = "基础对象修改接口")
    @Parameter(name = "driverContract", description = "修改的对象", required = true)
    @PutMapping
    public R<Void> update(@RequestBody @Valid DriverContract driverContract) {
        return R.success(driverContractService.updateById(driverContract));
    }

    //删除对象
    @Operation(summary = "删除DriverContract", description = "基础对象删除接口，采用状态删除")
    @Parameter(name = "id", description = "删除的对象ID", required = true)
    @DeleteMapping(value = "/{id}")
    public R<Void> delete(@PathVariable("id") Long id) {
        return R.success(driverContractService.removeById(id));
    }

    //获取对象
    @Operation(summary = "获取DriverContract", description = "基础对象获取接口")
    @Parameter(name = "id", description = "查询的对象ID", required = true)
    @GetMapping(value = "/{id}")
    public R<DriverContract> get(@PathVariable("id") Long id) {
        return R.success(driverContractService.getById(id));
    }

    //获取列表:PageQueryWrapper作为通用的查询对象
    @Operation(summary = "查询DriverContract列表", description = "基础对象列表查询，不带分页")
    @Parameter(name = "query", description = "查询条件", required = true)
    @PostMapping(value = "/list")
    public R<List<DriverContract>> list(@RequestBody PageQueryWrapper<DriverContract> query) {
        QueryWrapper<DriverContract> wrapper = new QueryWrapper<>();
        //实体类作为查询条件
        wrapper.setEntity(query.getQuery());
        return R.success(driverContractService.list(wrapper));
    }

    //分页查询
    @Operation(summary = "查询DriverContract分页列表", description = "基础对象列表查询，带分页")
    @Parameter(name = "query", description = "查询条件，需要指定分页条件", required = true)
    @PostMapping(value = "/pagelist")
    public R<PageResult<DriverContract>> page(@RequestBody PageQueryWrapper<DriverContract> query) {
        //分页查询
        Page<DriverContract> page = new Page<DriverContract>(query.getPage(), query.getRows());
        QueryWrapper<DriverContract> wrapper = new QueryWrapper<>();
        //实体类作为查询条件
        wrapper.setEntity(query.getQuery());
        //分页查询
        page = driverContractService.page(page, wrapper);
        //返回结果
        return R.success(new PageResult<DriverContract>(page.getTotal(), page.getRecords()));
    }

}
